package leetcode_1000;

import java.util.Arrays;

/**
 *@author 周杨
 *RevealCardsInIncreasingOrder_950 翻卡片游戏 每次翻开顶上的一张 下一张放在最底下 问如何排列卡片能让揭开卡片的排列递增
 *describe:用递归做 每次把偶数列处理 奇数列正常排列
 *2018年12月2日 下午6:32:45
 */
public class RevealCardsInIncreasingOrder_950 {
	
	public static void main(String[] args) {
		RevealCardsInIncreasingOrder_950 test=new RevealCardsInIncreasingOrder_950();
		//int []deck=test.deckRevealedIncreasing(new int[] {17,13,11,2,3,5,7});
		int []deck=test.deckRevealedIncreasing(new int[] {1,2,3,4,5});
		for(int i:deck)
			System.out.print(i+" ");
		
	}
	public int[] deckRevealedIncreasing(int[] deck) {
	      if(deck.length<=2) return deck;
	      Arrays.sort(deck);
	      int []temp= Arrays.copyOfRange(deck, (deck.length+1)/2, deck.length);
	      temp=deckRevealedIncreasing(temp);
	      
	      if(deck.length%2==1) {
	    	  int t=temp[temp.length-1];
		      for(int i=temp.length-1;i>=1;--i) {
		    	  temp[i]=temp[i-1];
		      }
	    	  temp[0]=t;
	      }
	      for(int i=(deck.length-1)%2==0?deck.length-1:deck.length-2;i>=0;i-=2) {
	    	  deck[i]=deck[i/2];
	      	}
	      for(int i=1;i<deck.length;i+=2) {
	    	  deck[i]=temp[i/2];
	      }
	      return deck;
	 }
}
